﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace DuAnNH.Fd_class
{
    class Cls_HeThong
    {
        public string id_user;
        public string user;
        public string password;
        public string chuoi_chucnang;
        public string loai_nhomnguoi;

        public DataTable load_tree_chucnang(TreeView tree)
        {
            try
            {
                string str = "Select * from TBL_Quyen";
                SqlDataAdapter sdap = new SqlDataAdapter(str, Coso.con);
                DataTable DT = new DataTable();
                sdap.Fill(DT);
                if (DT.Rows.Count > 0)
                {
                    for (int i = 0; i < DT.Rows.Count; i++)
                    {
                        TreeNode node = new TreeNode();
                        node.Tag = DT.Rows[i][0].ToString();
                        node.Text = DT.Rows[i][1].ToString();
                        tree.Nodes.Add(node);
                    }
                }
                return DT;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }
        public DataTable get_chucnang_user(string user, string pass)
        {
            try
            {
                string str = "sp_hethong_chucnang_select_user '" + user + "','" + pass + "'";
                SqlDataAdapter sdap = new SqlDataAdapter(str, Coso.con);
                DataTable DT = new DataTable();
                sdap.Fill(DT);
                return DT;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }
        public bool Cap_Nhat(string sqlstring)
        {
            try
            {
                SqlCommand com = new SqlCommand(sqlstring, Coso.con);
                com.ExecuteNonQuery();
                MessageBox.Show("Đã cập nhật thành công!");
                return true;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, "Lỗi Nhập mới!!!", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                return false;
            }
        }
        public bool update_user()
        {
            string str = "sp_hethong_user_update '" + chuoi_chucnang + "','" + loai_nhomnguoi + "','" + id_user + "'";
            return Cap_Nhat(str);
        }

        public void load_lstUser(ListView lst)
        {
            try
            {
                string str = "Select * from NhanVien";
                SqlDataAdapter sdap = new SqlDataAdapter(str, Coso.con);
                DataTable DT = new DataTable();
                sdap.Fill(DT);
                lst.Items.Clear();
                if (DT.Rows.Count > 0)
                {
                    for (int i = 0; i < DT.Rows.Count; i++)
                    {
                        ListViewItem Item = new ListViewItem();
                        Item.SubItems[0].Text = DT.Rows[i][1].ToString();
                        Item.SubItems.Add(DT.Rows[i][0].ToString());
                        Item.SubItems.Add(DT.Rows[i][2].ToString());
                        Item.SubItems.Add(DT.Rows[i][3].ToString());
                        lst.Items.Add(Item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
